package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbActivityInfo;
import com.ruoyi.system.mapper.TbActivityMapper;
import com.ruoyi.system.domain.TbActivity;
import com.ruoyi.system.service.ITbActivityService;

/**
 * 亲社会行为Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbActivityServiceImpl implements ITbActivityService 
{
    @Autowired
    private TbActivityMapper tbActivityMapper;

    /**
     * 查询亲社会行为
     * 
     * @param id 亲社会行为主键
     * @return 亲社会行为
     */
    @Override
    public TbActivity selectTbActivityById(Long id)
    {
        return tbActivityMapper.selectTbActivityById(id);
    }

    /**
     * 查询亲社会行为列表
     * 
     * @param tbActivity 亲社会行为
     * @return 亲社会行为
     */
    @Override
    public List<TbActivity> selectTbActivityList(TbActivity tbActivity)
    {
        return tbActivityMapper.selectTbActivityList(tbActivity);
    }

    /**
     * 新增亲社会行为
     * 
     * @param tbActivity 亲社会行为
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbActivity(TbActivity tbActivity)
    {
        tbActivity.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbActivity.getImages() != null && tbActivity.getImages().contains(",")) {
            tbActivity.setImages(tbActivity.getImages().split(",")[0]);
        }
        int rows = tbActivityMapper.insertTbActivity(tbActivity);
        insertTbActivityInfo(tbActivity);
        return rows;
    }

    /**
     * 修改亲社会行为
     * 
     * @param tbActivity 亲社会行为
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbActivity(TbActivity tbActivity)
    {
        tbActivity.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbActivity.getImages() != null && tbActivity.getImages().contains(",")) {
            tbActivity.setImages(tbActivity.getImages().split(",")[0]);
        }
        tbActivityMapper.deleteTbActivityInfoByActivityId(tbActivity.getId());
        insertTbActivityInfo(tbActivity);
        return tbActivityMapper.updateTbActivity(tbActivity);
    }

    /**
     * 批量删除亲社会行为
     * 
     * @param ids 需要删除的亲社会行为主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbActivityByIds(Long[] ids)
    {
        tbActivityMapper.deleteTbActivityInfoByActivityIds(ids);
        return tbActivityMapper.deleteTbActivityByIds(ids);
    }

    /**
     * 删除亲社会行为信息
     * 
     * @param id 亲社会行为主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbActivityById(Long id)
    {
        tbActivityMapper.deleteTbActivityInfoByActivityId(id);
        return tbActivityMapper.deleteTbActivityById(id);
    }

    /**
     * 新增亲社会行为详情信息
     * 
     * @param tbActivity 亲社会行为对象
     */
    public void insertTbActivityInfo(TbActivity tbActivity)
    {
        List<TbActivityInfo> tbActivityInfoList = tbActivity.getTbActivityInfoList();
        Long id = tbActivity.getId();
        if (StringUtils.isNotNull(tbActivityInfoList))
        {
            List<TbActivityInfo> list = new ArrayList<TbActivityInfo>();
            for (TbActivityInfo tbActivityInfo : tbActivityInfoList)
            {
                tbActivityInfo.setActivityId(id);
                list.add(tbActivityInfo);
            }
            if (list.size() > 0)
            {
                tbActivityMapper.batchTbActivityInfo(list);
            }
        }
    }
}
